/**
 * 3
 *  
 * @Author : Ruoyu Wang
 * @DateTime : 24-9-21 下午7:24
 */

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>

using namespace std;

struct Demand {
    string id;
    int startTime;
    int people;
    int time;

    Demand(string i, int r, int m, int d) : id(i), startTime(r), people(m), time(d) {}
};

struct Group {
    int id;
    int people;
    int days;

    Group(int i, int m) : id(i), people(m), days(0) {}

    bool operator<(const Group &g) const {
        if (days == g.days) return id > g.id;
        return days > g.days;
    }
};

int main() {
    string groupStr;
    getline(cin, groupStr);
    vector<Group> groups;
    vector<Demand> demands;

    int gid = 1;
    stringstream ss(groupStr);
    string tmp;
    while (getline(ss, tmp, '|')) {
        groups.push_back({gid++, stoi(tmp)});
    }
    while (getline(cin, tmp)) {
        if (tmp.empty()) break;
        int p1 = tmp.find('|');
        int p2 = tmp.find('|', p1 + 1);
        int p3 = tmp.find('|', p2 + 1);
        string id = tmp.substr(0, p1);
        int startTime = stoi(tmp.substr(p1 + 1, p2 - p1 - 1));
        int people = stoi(tmp.substr(p2 + 1, p3 - p2 - 1));
        int time = stoi(tmp.substr(p3 + 1));
        demands.push_back({id, startTime, people, time});
    }

    sort(demands.begin(), demands.end(), [](const Demand &a, const Demand &b) {
        return a.startTime < b.startTime;
    });

    priority_queue<Group> pq;

    for (auto &g: groups) pq.push(g);

    for (auto &d: demands) {
        while (!pq.empty()) {
            Group g = pq.top();
            pq.pop();
            if (g.people >= d.people) {
                int peoples = g.people;
                int actualTime = (d.people * d.time + peoples - 1) / peoples;
                int startTime = max(g.days, d.startTime);
                cout << d.id << "|" << g.id << "|" << startTime << "|" << actualTime << endl;
                g.days = startTime + actualTime;
                pq.push(g);
                break;
            } else {
                pq.push(g);
            }
        }
    }

    return 0;
}